package com.example.controller;

import com.example.pojo.Apartment;
import com.example.pojo.Result;
import com.example.service.impl.ApartmentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Slf4j
@CrossOrigin
@RequestMapping("/apartment")
@RestController
public class ApartmentController {
    @Autowired
    private ApartmentServiceImpl apartmentService;

    @Autowired
    private HttpServletRequest request;

    //房东添加房源
    @PostMapping
    public Result addApartment(@RequestBody Apartment apartment){
        log.info("{}",apartment.getImage());
        if(apartment.getName()==null || apartment.getName()==""){
            return Result.error("请输入房源名称");
        }
        if(apartment.getLocation()==null || apartment.getLocation()==""){
            return Result.error("请输入房源位置");
        }
        if(apartment.getFitment()==null || apartment.getFitment()==""){
            return Result.error("请输入房源装修情况");
        }
        if(apartment.getArea()==null){
            return Result.error("请输入房源面积");
        }
        if(apartment.getRoomType()==null || apartment.getRoomType()==""){
            return Result.error("请输入房源户型");
        }
        if(apartment.getPrice()==null){
            return Result.error("请输入房源价格");
        }
        if(apartment.getTowards()==null || apartment.getTowards()==""){
            return Result.error("请输入房源朝向");
        }
        if(apartment.getHostInformation()==null || apartment.getHostInformation()==""){
            return Result.error("请输入房东信息");
        }
        if(apartment.getNumber()==null || apartment.getNumber()==""){
            return Result.error("请输入房东联系电话");
        }
        /*if(apartment.getImage()==null || apartment.getImage()==""){
            return Result.error("请上传房源图片");
        }*/
        apartmentService.add(apartment,request);
        return Result.success();
    }

    //显示房东房源（全部，未出租，已出租）
    @GetMapping
    public Result getApartment(String name,Integer cuType){
        log.info("{}",name);
        List<Apartment> apartmentList=apartmentService.getApartment(name,cuType,request);
        return Result.success(apartmentList);
    }

    //房东删除房源
    @DeleteMapping("/{id}")
    public Result delApartment(@PathVariable Integer id){
        apartmentService.delApartment(id,request);
        return Result.success();
    }

    //显示房源具体信息
    @GetMapping("/{id}")
    public Result getApartmentId(@PathVariable Integer id){
        Apartment apartment=apartmentService.getApartmentId(id);
        return Result.success(apartment);
    }

    //房东修改房源具体信息
    @PutMapping
    public Result setApartment(@RequestBody Apartment apartment){
        apartmentService.setApartment(apartment,request);
        return Result.success();
    }

    //首页房源显示
    @GetMapping("/index")
    public Result getApartmentIndex(String name,String location,String fitment,String area,String roomType,String price,String towards,String subway,Double areaMax,Double areaMin,Double priceMin,Double priceMax){
        log.info("{}",area);
        log.info("{}",fitment);
        if(area!=null & area!=""){
            String[] split = area.split("-");
            areaMin = Double.parseDouble(split[0]);
            areaMax = Double.parseDouble(split[1]);
        }

        if(price!=null & price!=""){
            String[] split1 = price.split("-");
            priceMin = Double.parseDouble(split1[0]);
            priceMax = Double.parseDouble(split1[1]);
        }
        List<Apartment> list=apartmentService.getApartmentIndex(name,location,fitment,areaMin,areaMax,roomType,priceMin,priceMax,towards,subway);
        return Result.success(list);
    }

}
